package com.ruoyi.gczl.bzgf.ws.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gczl.bzgf.ws.domain.Bzgfws;
import com.ruoyi.gczl.bzgf.ws.service.IBzgfwsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 标准规范无损Controller
 *
 * @author huacai
 * @date 2024-08-28
 */
@RestController
@RequestMapping("/bzgf/bzgfws")
public class BzgfwsController extends BaseController {
    @Resource
    private IBzgfwsService bzgfwsService;

    /**
     * 查询标准规范无损列表
     */
    @PreAuthorize("@ss.hasPermi('bzgf:bzgfws:list')")
    @GetMapping("/list")
    public TableDataInfo list(Bzgfws bzgfws) {
        startPage();
        List<Bzgfws> list = bzgfwsService.selectBzgfwsList(bzgfws);
        return getDataTable(list);
    }

    /**
     * 导出标准规范无损列表
     */
    @PreAuthorize("@ss.hasPermi('bzgf:bzgfws:export')")
    @Log(title = "标准规范无损", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bzgfws bzgfws) {
        List<Bzgfws> list = bzgfwsService.selectBzgfwsList(bzgfws);
        ExcelUtil<Bzgfws> util = new ExcelUtil<Bzgfws>(Bzgfws.class);
        util.exportExcel(response, list, "标准规范无损数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Bzgfws> util = new ExcelUtil<Bzgfws>(Bzgfws.class);
        util.importTemplateExcel(response, "标准规范无损数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "标准规范无损", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('bzgf:bzgfws:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Bzgfws> util = new ExcelUtil<Bzgfws>(Bzgfws.class);
        InputStream inputStream = file.getInputStream();
        List<Bzgfws> list = util.importExcel(inputStream);
        inputStream.close();
        int count = bzgfwsService.batchInsertBzgfws(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取标准规范无损详细信息
     */
    @PreAuthorize("@ss.hasPermi('bzgf:bzgfws:query')")
    @GetMapping(value = "/{bzgfwsId}")
    public AjaxResult getInfo(@PathVariable("bzgfwsId") Integer bzgfwsId) {
        return success(bzgfwsService.selectBzgfwsByBzgfwsId(bzgfwsId));
    }

    /**
     * 新增标准规范无损
     */
    @PreAuthorize("@ss.hasPermi('bzgf:bzgfws:add')")
    @Log(title = "标准规范无损", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bzgfws bzgfws) {
        bzgfws.setUserId(Math.toIntExact(getUserId()));
        bzgfws.setDeptId(Math.toIntExact(getDeptId()));
        bzgfws.setCreateBy(getUsername());

        return toAjax(bzgfwsService.insertBzgfws(bzgfws));
    }

    /**
     * 修改标准规范无损
     */
    @PreAuthorize("@ss.hasPermi('bzgf:bzgfws:edit')")
    @Log(title = "标准规范无损", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bzgfws bzgfws) {
        return toAjax(bzgfwsService.updateBzgfws(bzgfws));
    }

    /**
     * 删除标准规范无损
     */
    @PreAuthorize("@ss.hasPermi('bzgf:bzgfws:remove')")
    @Log(title = "标准规范无损", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bzgfwsIds}")
    public AjaxResult remove(@PathVariable Integer[] bzgfwsIds) {
        return toAjax(bzgfwsService.deleteBzgfwsByBzgfwsIds(bzgfwsIds));
    }
}
